﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineMovieTicket.Data;
using OnlineMovieTicket.Model;
using OnlineMovieTicket.Data.IRepository;

namespace OnlineMovieTicket.Data.Repository
{
    public class TransactionRepository : ITransactionRepository
    {
        ONLINEMOVIETICKETEntities dataContent;
        
        public TransactionRepository()
        { dataContent = new ONLINEMOVIETICKETEntities(); }

        

        public List<Model.TransactionDetail> getMovieBookingDetail(int movieAssignmentID)
        {
          
            
             var   data = (from d in dataContent.Transactions
                            where d.MovieAssignmentID == movieAssignmentID
                            select new Model.TransactionDetail

                             {
                                 Amount = (Double)d.Amount,
                                 CustomerName = d.CustomerName,
                                 Email = d.Email,
                                 PhoneNo = d.PhoneNo,
                                 Status = d.Status,
                                 TransactionDt = (System.DateTime)d.TransactionDateTime,
                                 TransactionID = d.TransactionID,
                                 movieName = d.MovieTimeTable.Movie.MovieName,
                             }).ToList();
             foreach (Model.TransactionDetail trans in data)
             {
                 var seatList = (from seat in dataContent.TransactionDetails
                                 where seat.TransactionID == trans.TransactionID
                                 select seat.SeatNumber).ToList();

                 List<String> seatNumList = new List<string>();

                 foreach (String seatNum in seatList)
                 {
                     seatNumList.Add(seatNum);
                 }

                 trans.SeatNum = seatNumList;

             }

                return data;
            
            
           
        }

        public Model.TransactionDetail getTransactionDetail(int transactionId)
        {
            var data = (from d in dataContent.Transactions
                        where d.TransactionID == transactionId
                        select new Model.TransactionDetail
                        {
                            movieName = d.MovieTimeTable.Movie.MovieName,
                            ShowDateTime = d.MovieTimeTable.CinemaTimeTable.StartTime,
                            Amount = (Double)d.Amount
                        }).First();


            var seatList = (from seat in dataContent.TransactionDetails
                            where seat.TransactionID == transactionId
                            select seat.SeatNumber).ToList();

            List<String> seatNumList = new List<string>();

            foreach (String seatNum in seatList)
            {
                seatNumList.Add(seatNum);
            }

            data.SeatNum = seatNumList;


            return data;
        }


        public Model.TransactionDetail getMovieDetail(int movieAssignmentID)
        {
            var data = (from d in dataContent.MovieTimeTables
                        where d.MovieAssignmentID == movieAssignmentID
                        select new Model.TransactionDetail
                        {
                            movieName = d.Movie.MovieName,
                            ShowDateTime = d.CinemaTimeTable.StartTime
                        }).First();

            return data;
        }



        public void resetPendingBooking()
        {
            System.DateTime fiveMinBeforeCurrentTime = System.DateTime.Now.AddMinutes(-1);
            String pendingStatus = "P";
            var deletingData = (from d in dataContent.Transactions
                        where d.TransactionDateTime < fiveMinBeforeCurrentTime
                        && d.Status == pendingStatus
                        select d);

            foreach (OnlineMovieTicket.Data.Transaction items in deletingData)
            {
                var deletingDetailData = (from d in dataContent.TransactionDetails
                                          where d.TransactionID == items.TransactionID
                                          select d);
                foreach(OnlineMovieTicket.Data.TransactionDetail itemDetails in deletingDetailData)
                {
                    dataContent.TransactionDetails.DeleteObject(itemDetails);
                }
                dataContent.Transactions.DeleteObject(items);
                
            }
            dataContent.SaveChanges();
        }
        
        public void bookMovieTicket(Model.TransactionDetail transactionDetail)
        {
            dataContent.AddToCustomers(new Customer { Email = transactionDetail.Email, NIRC = transactionDetail.NRIC, PhoneNo = transactionDetail.PhoneNo, Status = transactionDetail.Status });
            dataContent.SaveChanges();

            var transation = (from d in dataContent.Transactions
                              where d.TransactionID == transactionDetail.TransactionID
                              select d).First();

            transation.Status = "A";
            transation.NRIC = transactionDetail.NRIC;
            transation.PhoneNo = transactionDetail.PhoneNo;
            transation.Email = transactionDetail.Email;
            transation.CustomerName = transactionDetail.CustomerName;
            dataContent.SaveChanges();
            
            
        }

        public Model.TransactionDetail saveTransaction(int movieAssignId)
        {
            Transaction trans = new Transaction();


            trans.Status = "P";
            trans.TransactionDateTime = System.DateTime.Now;
            trans.MovieAssignmentID = movieAssignId;
            trans.Amount = 0.0;

            dataContent.AddToTransactions(trans);
            dataContent.SaveChanges();
            Model.TransactionDetail modelTran = new Model.TransactionDetail();
            modelTran.TransactionID = trans.TransactionID;
            return modelTran;

        }


        public Model.TransactionDetail saveTransaction(int transactionId, String seatNum, String status, String uniqueId, int movieAssignId)

        {
               var transaction =(from d in dataContent.Transactions 
                                  where d.TransactionID == transactionId select d);
               Transaction trans = null;

               if (transaction.Count() == 0)
               {
                   trans = new Transaction();
                   System.Data.Objects.DataClasses.EntityCollection<TransactionDetail> transDetail = new System.Data.Objects.DataClasses.EntityCollection<TransactionDetail>();
                   TransactionDetail seatDetail = new TransactionDetail();
                   seatDetail.SeatNumber = seatNum;
                   transDetail.Add(seatDetail);

                   trans.Status = "P";
                   trans.Amount = 10.0;
                   trans.TransactionDateTime = System.DateTime.Now;
                   trans.MovieAssignmentID = movieAssignId;
                   trans.TransactionDetails=transDetail;
                   dataContent.AddToTransactions(trans);
                   

               }
               else
               {

                   if (status == "")
                   {
                       transaction.First().TransactionDetails.Add(new TransactionDetail { TransactionID = transaction.First().TransactionID, SeatNumber = seatNum });
                       transaction.First().Amount = transaction.First().Amount + 10.0;
                   }
                   else
                   {
                       var deletingTran= (from transDel in dataContent.TransactionDetails
                                           where transDel.SeatNumber == seatNum && transDel.TransactionID == transactionId select transDel).First();

                       transaction.First().TransactionDetails.Remove(deletingTran);
                       transaction.First().Amount = transaction.First().Amount - 10.0;
                   }
               }
               dataContent.SaveChanges();

               Model.TransactionDetail transModel = new Model.TransactionDetail();
               if (trans == null)
               {
                   transModel.Amount = (Double)transaction.First().Amount;
                   transModel.TransactionID = transaction.First().TransactionID;
               }
               else
               {
                   transModel.Amount = (Double)trans.Amount;
                   transModel.TransactionID = trans.TransactionID;
               }


               return transModel;
        }

        public void updateTransactionTime(int transactionID)
        {
            DateTime currentTime=DateTime.Now;
            var transation = (from d in dataContent.Transactions
                              where d.TransactionID == transactionID
                              select d).First();
            transation.TransactionDateTime = currentTime;
            dataContent.SaveChanges();
        }

        public List<Model.TransactionDetail> getCheckBookingByNric(String nric)
        {
            var data = (from d in dataContent.Transactions
                        where d.NRIC == nric & d.Status == "A"
                        select new Model.TransactionDetail

                        {
                            Amount = (Double)d.Amount,
                            CustomerName = d.CustomerName,
                            Email = d.Email,
                            PhoneNo = d.PhoneNo,
                            Status = d.Status,
                            TransactionDt = (System.DateTime)d.TransactionDateTime,
                            TransactionID = d.TransactionID,
                            movieName = d.MovieTimeTable.Movie.MovieName,
                        }).ToList();
            
            foreach (Model.TransactionDetail trans in data)
            {
                var seatList = (from seat in dataContent.TransactionDetails
                                where seat.TransactionID == trans.TransactionID
                                select seat.SeatNumber).ToList();

                List<String> seatNumList = new List<string>();

                foreach (String seatNum in seatList)
                {
                    seatNumList.Add(seatNum);
                }

                trans.SeatNum = seatNumList;

            }

            return data;
            
        }

        public List<Model.TransactionDetail> getCheckBookingByVerificationID(int verificationId)
        {
            var transaction = (from d in dataContent.Transactions
                               where d.TransactionID == verificationId
                               select new Model.TransactionDetail
                               {
                                   Amount = (Double)d.Amount,
                                   CustomerName = d.CustomerName,
                                   Email = d.Email,
                                   PhoneNo = d.PhoneNo,
                                   Status = d.Status,
                                   TransactionDt = (System.DateTime)d.TransactionDateTime,
                                   TransactionID = d.TransactionID,
                                   movieName = d.MovieTimeTable.Movie.MovieName,
                                   SeatNum = (from seat in dataContent.TransactionDetails
                                              where seat.TransactionID == d.TransactionID
                                              select seat.SeatNumber).ToList(),
                               }).ToList();
            return transaction;
        }

        public List<Model.TransactionDetail> getCinemasMovieDetail(Model.TransactionDetail movieDetail)
        {
            
            List<OnlineMovieTicket.Data.MovieTimeTable>
                allMovie = (from movie in dataContent.MovieTimeTables  
                            select movie
                            ).ToList();

            String currentMovie = "";
            String cinemasHallName = "";
            
            if (movieDetail.ShowDateTime.CompareTo(System.DateTime.Now) != -1)
            {
                allMovie = allMovie.Where(m => m.ShowingDate == movieDetail.ShowDateTime).ToList();
            }
            if (movieDetail.MovieCode != 0)
            {
                allMovie = allMovie.Where(movie => movie.MovieCode == movieDetail.MovieCode).ToList() ;
            }
            if (movieDetail.cinemaId != 0)
            {

                allMovie = allMovie.Where(movie => movie.CinemaTimeTable.CinemaHall.CinemaID == movieDetail.cinemaId).ToList();
            }

            var movieList = (from c in allMovie
                             orderby c.CinemaTimeTable.CinemaHall.CinemaHallName, c.Movie.MovieName,c.CinemaTimeTable.StartTime
                             select c).ToList();

            List<Model.TransactionDetail> modelList = new List<Model.TransactionDetail>();
            
            Model.TransactionDetail transDetail = new Model.TransactionDetail();
            transDetail.Movies = new List<TransactionMovieDetail>();

            Model.TransactionMovieDetail movieTranDetail = new Model.TransactionMovieDetail();

            movieTranDetail.startTimeList = new List<Model.MovieTimeTable>();
            

            foreach (OnlineMovieTicket.Data.MovieTimeTable m in movieList)
            {

                if (cinemasHallName.Equals("") || (cinemasHallName.Equals(m.CinemaTimeTable.CinemaHall.CinemaHallName)))
                {
                    cinemasHallName = m.CinemaTimeTable.CinemaHall.CinemaHallName;
                    transDetail.CinemasName = cinemasHallName;
                    movieTranDetail.MovieName = m.Movie.MovieName;
                    if (currentMovie.Equals("") || currentMovie.Equals(m.Movie.MovieName))
                    {
                        Model.MovieTimeTable timeTranDetail = new Model.MovieTimeTable();

                        timeTranDetail.StartTime = m.CinemaTimeTable.StartTime;
                        
                        timeTranDetail.MovieAssignmentID= m.MovieAssignmentID;

                        movieTranDetail.startTimeList.Add(timeTranDetail);
                    }
                    else
                    {
                        transDetail.Movies.Add(movieTranDetail);
                        movieTranDetail = new Model.TransactionMovieDetail();
                        movieTranDetail.startTimeList = new List<Model.MovieTimeTable>();
                        Model.MovieTimeTable timeTranDetail = new Model.MovieTimeTable();
                        timeTranDetail.StartTime = m.CinemaTimeTable.StartTime;
                        timeTranDetail.MovieAssignmentID = m.MovieAssignmentID;
                        movieTranDetail.startTimeList.Add(timeTranDetail);
                        currentMovie = m.Movie.MovieName;
                    }
                }
                else
                {
                    transDetail.Movies.Add(movieTranDetail);
                    modelList.Add(transDetail);

                    transDetail = new Model.TransactionDetail();
                    transDetail.Movies = new List<TransactionMovieDetail>();
                    movieTranDetail = new Model.TransactionMovieDetail();
                    movieTranDetail.startTimeList = new List<Model.MovieTimeTable>();
                    Model.MovieTimeTable timeTranDetail = new Model.MovieTimeTable();

                    timeTranDetail.StartTime = m.CinemaTimeTable.StartTime;
                    timeTranDetail.MovieAssignmentID = m.MovieAssignmentID;

                    movieTranDetail.startTimeList.Add(timeTranDetail);
                    currentMovie = m.Movie.MovieName;
                    cinemasHallName = m.CinemaTimeTable.CinemaHall.CinemaHallName;
                    movieTranDetail.MovieName = currentMovie;
                    transDetail.CinemasName = cinemasHallName;

                }
            }

            transDetail.Movies.Add(movieTranDetail);
            modelList.Add(transDetail);
            return modelList;
        }


        public List<Model.TransactionDetail> getUserTransactionDetail(Model.Customer customerDetail)
        {
            var transaction = (from t in dataContent.TransactionDetails
                              where t.Transaction.NRIC == customerDetail.NRIC
                              select new Model.TransactionDetail 
                              {
                                  CustomerName = t.Transaction.CustomerName,
                                  NRIC = t.Transaction.NRIC,
                                  PhoneNo = t.Transaction.PhoneNo,
                                  Email =t.Transaction.Email,
                              }).ToList();

            return transaction;
        }

        public int getMaxTransactionNum()
        {
            int maxNum = 0;
            return maxNum;
        }
        
    }
}
